package com.example.test_witns.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;


public class CenterThickScanLineView extends View {
    private Paint paint;
    private Path path;
    private int centerColor = 0x9900FF00;  // 中心颜色（绿色）
    private int edgeColor = 0x0000FF00;     // 边缘颜色（透明绿）
    private float progress;                // 动画进度 (0~1)
    private float lineWidth;               // 扫描线最大宽度

    public CenterThickScanLineView(Context context) {
        super(context);
        init();
    }

    public CenterThickScanLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        path = new Path();
        lineWidth = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 
            400,  // 最大宽度 8dp
            getResources().getDisplayMetrics()
        );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float centerY = progress * getHeight();
        float halfWidth = 400;

        // 构建扁平化路径（控制点高度降低）
        path.reset();
        path.moveTo(getWidth()/2 - halfWidth, centerY);
        path.quadTo(
                getWidth()/2,
                centerY - halfWidth * 0.03f, // 控制点高度减少 70%
                getWidth()/2 + halfWidth,
                centerY
        );
        path.quadTo(
                getWidth()/2,
                centerY + halfWidth * 0.0f,
                getWidth()/2 - halfWidth,
                centerY
        );
        path.close();

        // 缩小渐变范围
        float gradientRadius = halfWidth * 1.2f; // 仅扩大 20%
        Shader shader = new RadialGradient(
                getWidth()/2f,
                centerY,
                gradientRadius,
                new int[]{0xFF00FF00, 0x2000FF00, 0x0000FF00}, // 增加中间过渡色
                new float[]{0f, 0.5f, 1f},
                Shader.TileMode.CLAMP
        );
        paint.setShader(shader);

        canvas.drawPath(path, paint);
    }

    // 根据Y位置计算宽度（中间最宽）
    private float calculateCurrentWidth(float y) {
        float center = getHeight() / 2f;
        if (center <= 0) return 1f; // 防止除以零

        float distance = Math.abs(y - center);
        float width = lineWidth * (1 - distance / center);
        return Math.max(200, 1f); // 确保最小宽度
    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }
}